#encoding: utf-8
class CulturalHeritage::CulturesController < ApplicationController
  #before_filter :authenticate_user!
  # GET /cultural_heritage/cultures
  # GET /cultural_heritage/cultures.xml
  def index
    @title_view = 'Culturas'
    @cultural_heritage_cultures = CulturalHeritage::Culture.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @cultural_heritage_cultures }
      format.json  { render :json => @cultural_heritage_cultures }
    end
  end

  # GET /cultural_heritage/cultures/1
  # GET /cultural_heritage/cultures/1.xml
  def show
    @title_view = 'Culturas'
    @cultural_heritage_culture = CulturalHeritage::Culture.find(params[:id])

    points = @cultural_heritage_culture.list_point

    if points.length > 0

      count = 0
      @json = "[["
      points.each do |point|
        if (count == 0)
          @json = @json << '{"lng": ' << point.longitude.to_s << ', "lat": ' << point.latitude.to_s <<
            ', "strokeColor": "#FF0000", "strokeOpacity": 0.3, "strokeWeight": 1, "fillColor": "#FF0000", "fillOpacity": 0.7}'
        else
          @json = @json << ', {"lng": ' << point.longitude.to_s << ', "lat": ' << point.latitude.to_s << '}'
        end
        count = count + 1
      end
      @json = @json << "]]"

    end

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @cultural_heritage_culture }
      format.json  { render :json => @cultural_heritage_culture }
    end
  end

  # GET /cultural_heritage/cultures/new
  # GET /cultural_heritage/cultures/new.xml
  def new
    if (user_signed_in? && ([2].include?(current_user.role)))
      @title_view = 'Culturas'
      @cultural_heritage_culture = CulturalHeritage::Culture.new
      @periods = CulturalHeritage::Period.all
      respond_to do |format|
        format.html # new.html.erb
        format.xml  { render :xml => @cultural_heritage_culture }
      end
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  # GET /cultural_heritage/cultures/1/edit
  def edit
    if (user_signed_in? && ([2].include?(current_user.role)))
    @title_view = 'Culturas'
    @cultural_heritage_culture = CulturalHeritage::Culture.find(params[:id])
    @periods = CulturalHeritage::Period.all
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  # POST /cultural_heritage/cultures
  # POST /cultural_heritage/cultures.xml
  def create
    if (user_signed_in? && ([2].include?(current_user.role)))
    @title_view = 'Culturas'
    @cultural_heritage_culture = CulturalHeritage::Culture.new(params[:cultural_heritage_culture])
    @periods = CulturalHeritage::Period.all
    respond_to do |format|
      if @cultural_heritage_culture.save
        format.html { redirect_to(@cultural_heritage_culture, :notice => 'Se ha creado correctamente la cultura.') }
        format.xml  { render :xml => @cultural_heritage_culture, :status => :created, :location => @cultural_heritage_culture }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @cultural_heritage_culture.errors, :status => :unprocessable_entity }
      end
    end
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  # PUT /cultural_heritage/cultures/1
  # PUT /cultural_heritage/cultures/1.xml
  def update
    if (user_signed_in? && ([2].include?(current_user.role)))
    @cultural_heritage_culture = CulturalHeritage::Culture.find(params[:id])
    @periods = CulturalHeritage::Period.all
    respond_to do |format|
      if @cultural_heritage_culture.update_attributes(params[:cultural_heritage_culture])
        format.html { redirect_to(@cultural_heritage_culture, :notice => 'Se ha actualizado correctamente la cultura.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @cultural_heritage_culture.errors, :status => :unprocessable_entity }
      end
    end
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  # DELETE /cultural_heritage/cultures/1
  # DELETE /cultural_heritage/cultures/1.xml
  def destroy
    if (user_signed_in? && ([2].include?(current_user.role)))
    @cultural_heritage_culture = CulturalHeritage::Culture.find(params[:id])
    @cultural_heritage_culture.deleted = 1
    @cultural_heritage_culture.save
    #@cultural_heritage_culture.destroy
    respond_to do |format|
      format.html { redirect_to(cultural_heritage_cultures_url) }
      format.xml  { head :ok }
    end
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end
  
  def update_table
    @term = params[:search_name]
    if !@term.blank?
      @cultural_heritage_cultures = CulturalHeritage::Culture.find(:all,:conditions=>["cultural_heritage_cultures.name LIKE ?",'%'+@term+'%'])
    else
      @cultural_heritage_cultures = CulturalHeritage::Culture.all
    end
    render :partial =>"table"
  end

  def list_points
    if (user_signed_in? && ([2].include?(current_user.role)))
      @culture = CulturalHeritage::Culture.find(params[:id])
      @culture_points = @culture.list_point
      respond_to do |format|
        format.html {render :layout => "blank"}# index.html.erb
        format.xml  { render :xml => @culture_points }
        format.json { render :json => @culture_points }
      end
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  def new_point
    if (user_signed_in? && ([2].include?(current_user.role)))
      @culture = CulturalHeritage::Culture.find(params[:id])
      @culture_point = CulturalHeritage::CulturePoint.new()
      respond_to do |format|
        format.html {render :layout => "blank"}# index.html.erb
        format.xml  { render :xml => @culture_point }
        format.json { render :json => @culture_point }
      end
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  def create_point
    if (user_signed_in? && ([2].include?(current_user.role)))
      @culture_point = CulturalHeritage::CulturePoint.new(params[:cultural_heritage_culture_point])
      respond_to do |format|
        if @culture_point.save
          format.html { redirect_to({:action => 'list_points',:id => @culture_point.culture}, :notice => 'Se añadió el punto exitósamente.') }
        else
          format.html { render :action => "new_point",:layout => "blank"}
        end
      end
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  def delete_point
    if (user_signed_in? && ([2].include?(current_user.role)))
      @culture_point = CulturalHeritage::CulturePoint.find(params[:id])
      @culture_point.deleted = 1
      @culture_point.save
      respond_to do |format|
        format.html { redirect_to(:action => 'list_points',:id => @culture_point.obj_culture.id,:layout => "blank") }
        format.xml  { head :ok }
      end
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  def draw_cultures
    lcultures = params[:lculture]
    if !(lcultures.nil? || lcultures.blank?)

      supercount = 0
      @json = "["
      lcultures.each do |aculture|

        culture = CulturalHeritage::Culture.find(aculture.to_i)

        points = culture.list_point
        if points.length > 0
          count = 0

          if (supercount == 0)
            @json = @json << "["
          else
            @json = @json << ", ["
          end
          points.each do |point|
            if (count == 0)
              @json = @json << '{"lng": ' << point.longitude.to_s << ', "lat": ' << point.latitude.to_s <<
                ', "strokeColor": '<< "#FF0000" <<', "strokeOpacity": 0.3, "strokeWeight": 1, "fillColor": "#FF0000", "fillOpacity": 0.7}'
            else
              @json = @json << ', {"lng": ' << point.longitude.to_s << ', "lat": ' << point.latitude.to_s << '}'
            end
            count = count + 1
          end
          @json = @json << "]"
        end

        supercount = supercount + 1
      end
      @json = @json << "]"

      respond_to do |format|
        format.html {render :layout => "blank"}# index.html.erb
        format.xml  { render :xml => @cultural_heritage_cultures }
        format.json { render :json => @cultural_heritage_cultures }
      end
    end
  end

  def map_search
    @title_view = 'Culturas'
    @cultural_heritage_cultures = CulturalHeritage::Culture.all
    
    @arr = Array.new    

    lcultures = params[:lculture]
    if !(lcultures.nil? || lcultures.blank?)
      supercount = 0
      @json = "["
      lcultures.each do |aculture|

        r = rand(255).to_s(16)
        g = rand(255).to_s(16)
        b = rand(255).to_s(16)
        r, b, g = [r, b, g].map { |s| if s.size == 1 then '0' + s else s end }
        colour = r + b + g

        culture = CulturalHeritage::Culture.find(aculture.to_i)
        @colour = colour
        points = culture.list_point
        if points.length > 0
          @arr << [culture.name, '#' << colour]
          count = 0          
          if (supercount == 0)
            @json = @json << "["
          else
            @json = @json << ", ["
          end
          points.each do |point|
            if (count == 0)
              @json = @json << '{"lng": ' << point.longitude.to_s << ', "lat": ' << point.latitude.to_s <<
                ', "strokeColor": "#' << colour << '", "strokeOpacity": 0.3, "strokeWeight": 1, "fillColor": "#' << colour << '", "fillOpacity": 0.7}'
            else
              @json = @json << ', {"lng": ' << point.longitude.to_s << ', "lat": ' << point.latitude.to_s << '}'
            end
            count = count + 1
          end
          @json = @json << "]"
        end

        supercount = supercount + 1
      end
      @json = @json << "]"    
    end

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @cultural_heritage_cultures }
      format.json  { render :json => @cultural_heritage_cultures }
    end
  end
end
